package com.prp.gwt.client.widgets.user.home;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.php.PHPListener;
import com.prp.gwt.client.vo.UserStateList;

public class UserStatePanel extends DecoratorPanel {

	private static UserStatePanel instance;
	private HorizontalPanel panel = new HorizontalPanel();
	private HorizontalPanel innerPanel = new HorizontalPanel();
	private Image image = new Image();
	private DisclosurePanel disclosurePanel = new DisclosurePanel("修改");
	private HTML name = new HTML();
	private VerticalPanel editPanel = new VerticalPanel();
	private TextBox textArea = new TextBox();
	private int MAX_WORD = 50;
	private Label count = new Label();
	private Label message = new Label("您最多能输入" + MAX_WORD + "个字符");
	private HTML content = new HTML();

	public static UserStatePanel getInstance(){
		return instance;
	}
	
	public UserStatePanel() {
		instance = this;
		addStyleName("state");
		panel.add(image);
		image.addStyleName("state-image");
		innerPanel.add(name);
		VerticalPanel rightPanel = new VerticalPanel();
		rightPanel.add(content);
		editPanel.add(textArea);
		HorizontalPanel wordCountPanel = new HorizontalPanel();
		wordCountPanel.addStyleName("state-panel-item");
		count.setText("0");
		wordCountPanel.add(count);
		wordCountPanel.add(new Label("/" + MAX_WORD));
		message.setVisible(false);
		wordCountPanel.add(message);
		editPanel.add(wordCountPanel);
		Button commit = new Button("发布");
		editPanel.add(commit);
		disclosurePanel.setContent(editPanel);
		rightPanel.add(disclosurePanel);
		innerPanel.add(rightPanel);
		panel.add(innerPanel);
		setWidget(panel);

		commit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (textArea.getText().isEmpty())
					Window.alert("输入不能为空");
				else
					PHPListener.getInstance().addState(textArea.getText());
			}
		});
		textArea.setVisibleLength(30);
		textArea.setMaxLength(MAX_WORD);
		textArea.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				count.setText("" + textArea.getText().length());
			}
		});
	}

	public void init() {
		name.setText("");
		content.setText("");
		disclosurePanel.setOpen(false);
		textArea.setText("");
		count.setText("0");
	}

	public void load(UserStateList userState) {
		image.setUrl(userState.getOwner().getPortrait());
		name.setHTML("<a>" + userState.getOwner().getUsername() + "</a>");
		name.addStyleName("state-name");
		content.addStyleName("state-content");
		content.setWordWrap(true);
		content.addStyleName("state-content");
		if (userState.get(0).getState() == null)
			content.setText("什么都没做");
		else
			content.setText(userState.get(0).getState());
	}

}
